package service

import (
	"github.com/gin-gonic/gin"
	"hanlinyuan_qa/model"
	"hanlinyuan_qa/util"
	"net/http"
)

func GetDeletedQuestion(c *gin.Context) {
	var data []model.Question
	model.GetDB().Model(data).Where("is_delete=?", 1).Order("create_time desc").Limit(100).Find(&data)
	if len(data) == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "垃圾箱是空的",
			"code": 0,
			"data": "",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"msg":  "获取垃圾箱问卷成功！",
		"data": data,
	})
}

func RecoverQuestion(c *gin.Context) {
	var getQuestionValidator GetQuestionValidator
	if err := c.ShouldBind(&getQuestionValidator); err == nil {
		qid := getQuestionValidator.QuestionID
		var data []model.Question
		model.GetDB().Model(data).Where("question_id=?", qid).Find(&data)
		if len(data) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "未找到对应的问卷",
				"code": 0,
				"data": "",
			})
			return
		}
		model.GetDB().Model(data).Where("question_id=?", data[0].QuestionID).Updates(
			map[string]interface{}{
				"is_delete": false,
			},
		)
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "恢复成功！",
			"data": "",
		})
		/**
		model.GetDB().Model(dataQuestion).Where("question_id=?", dataQuestion[0].QuestionID).Updates(
			map[string]interface{}{
				"update_time": time.Now(),
				"is_activate": val.IsActivate == "true",
				"content":     val.Content,
			},
		)
		*/
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  err.Error(),
			"code": 0,
			"data": gin.H{},
		})
	}
}

type GetAnswerDetailValidator struct {
	AnswerID int `form:"aid" binding:"required"`
}

func DeleteQuestion(c *gin.Context) {
	var getQuestionValidator GetQuestionValidator
	if err := c.ShouldBind(&getQuestionValidator); err == nil {
		qid := getQuestionValidator.QuestionID
		var data []model.Question
		model.GetDB().Model(data).Where("question_id=?", qid).Find(&data)
		if len(data) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "未找到对应的问卷",
				"code": 0,
				"data": "",
			})
			return
		}
		if data[0].IsDelete {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "问卷已被删除",
				"code": 0,
				"data": "",
			})
			return
		}
		data[0].IsDelete = true
		model.GetDB().Model(data).Where("question_id=?", data[0].QuestionID).Updates(&data[0])
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "删除成功！",
			"data": "",
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  err.Error(),
			"code": 0,
			"data": gin.H{},
		})
	}
}

func GetAnswerDetail(c *gin.Context) {
	var val GetAnswerDetailValidator
	if err := c.ShouldBind(&val); err == nil {
		var dataTmp []model.Answer
		model.GetDB().Model(dataTmp).Where("answer_id=?", val.AnswerID).Find(&dataTmp)
		if len(dataTmp) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "没有查找到相关数据",
				"code": 0,
				"data": gin.H{},
			})
		}
		c.JSON(http.StatusOK, gin.H{
			"msg":  "查询成功！",
			"code": 0,
			"data": dataTmp[0],
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  err.Error(),
			"code": 0,
			"data": gin.H{},
		})
	}
}

type ShareDataValidator struct {
	QuestionID string `form:"qid" binding:"required"`
	Token      string `form:"token" binding:"required"`
	Offset     int    `form:"offset" binding:"gte=0"`
	Limit      int    `form:"limit" binding:"gte=0,lte=100"`
}

func GetShareData(c *gin.Context) {
	var val ShareDataValidator
	if err := c.ShouldBind(&val); err == nil {
		uid := util.GetIdOfToken(val.Token)
		if uid == -1 {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "Token解析失败",
				"code": 0,
				"data": "",
			})
			return
		}

		var data []model.Question
		model.GetDB().Model(data).Where("question_id=?", val.QuestionID).Find(&data)
		if len(data) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "未找到对应的问卷",
				"code": 0,
				"data": "",
			})
			return
		}
		if data[0].IsDelete {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "问卷已被删除",
				"code": 0,
				"data": "",
			})
			return
		}
		var dataAnswer []model.Answer
		var totalCount int64
		model.GetDB().Model(dataAnswer).Where(
			"question_id=?", val.QuestionID,
		).Order(
			"create_time desc",
		).Count(&totalCount).Offset(val.Offset).Limit(val.Limit).Find(&dataAnswer)

		c.JSON(http.StatusOK, gin.H{
			"msg":  "获取数据成功",
			"code": 0,
			"data": gin.H{
				"total":    totalCount,
				"question": data[0],
				"answers":  dataAnswer,
			},
		})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  err.Error(),
			"code": 0,
			"data": gin.H{},
		})
	}
}

type AdminGetTotalQuestionValidator struct {
	QuestionID string `form:"qid" binding:"required"`
	Offset     int    `form:"offset" binding:"gte=0"`
	Limit      int    `form:"limit" binding:"gte=0,lte=100"`
}

func AdminGetQuestionTotal(c *gin.Context) {
	var val AdminGetTotalQuestionValidator
	if err := c.ShouldBind(&val); err == nil {
		var data []model.Question
		model.GetDB().Model(data).Where("question_id=?", val.QuestionID).Find(&data)
		if len(data) == 0 {
			c.JSON(http.StatusOK, gin.H{
				"msg":  "未找到对应的问卷",
				"code": 0,
				"data": "",
			})
			return
		}
		//if data[0].IsDelete {
		//	c.JSON(http.StatusOK, gin.H{
		//		"msg":  "问卷已被删除",
		//		"code": 0,
		//		"data": "",
		//	})
		//	return
		//}
		var dataAnswer []model.Answer
		var totalCount int64
		model.GetDB().Model(dataAnswer).Where(
			"question_id=?", val.QuestionID,
		).Order(
			"create_time desc",
		).Count(&totalCount).Offset(val.Offset).Limit(val.Limit).Find(&dataAnswer)

		c.JSON(http.StatusOK, gin.H{
			"msg":  "获取数据成功",
			"code": 0,
			"data": gin.H{
				"total":    totalCount,
				"question": data[0],
				"answers":  dataAnswer,
			},
		})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  err.Error(),
			"code": 0,
			"data": gin.H{},
		})
	}

}

type AdminGetQuestionValidator struct {
	Offset int `form:"offset" binding:"gte=0"`
	Limit  int `form:"limit" binding:"gte=0,lte=100"`
}

func AdminGetQuestion(c *gin.Context) {
	var val AdminGetQuestionValidator
	if err := c.ShouldBind(&val); err == nil {
		var totalCount int64
		var data []model.Question
		model.GetDB().Model(data).Where("is_delete=?", 0).Order("create_time desc, is_activate desc").Count(&totalCount).Limit(val.Limit).Offset(val.Offset).Find(&data)
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "获取成功",
			"data": gin.H{
				"total": totalCount,
				"lists": data,
			},
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  err.Error(),
			"code": 0,
			"data": gin.H{},
		})
	}

}
